<script src="{{ getAssets }}js/amazeui.pagination.js?v={{ assets_version }}"></script>
<script>
var curr_page = {
    'lens' : 0,
    'mounting' : 0,
    'sunglass' : 0,
    'presbyopic_glass' : 0,
    'contact_lens' : 0,
    'contact_lens_care_solution' : 0,
    'other' : 0
};
var curr_typeid = {
    'lens' : 10,
    'mounting' : 30,
    'sunglass' : 41,
    'presbyopic_glass' : 40,
    'contact_lens' : 20,
    'contact_lens_care_solution' : 50,
    'other' : 60
};

var get_goods_url = '{{ urlfor "RetailController.Custom" }}';
var custom_product_list_save_url = '{{ urlfor "RetailController.CustomSave" }}';
var query_data = {};
$("input").on("keypress",function(e){
    if (e.which == 13) {
        query($(this).parents("form[data-type]").attr("data-type"));
    }
});
function query(goods_type){
    var data=$("form[data-type='"+goods_type+"']").serializeObject();
    var reserve_num=[];
    reserve_num.push("1");
    data.goods_type=goods_type;
    data.reserve_num=reserve_num;
    query_data = data;
    ajax_database_products_list(goods_type);
}
function ajax_database_products_list(goods_type) {
    query_data.offset = curr_page[goods_type] * query_data.limit;
    query_data.typeid = curr_typeid[goods_type];
    query_data.limit = 18;
    var box = $('form[data-type="'+ goods_type +'"]').find('#'+ goods_type +'_datalist');
    $.post(get_goods_url, JSON.stringify(query_data), function (res) {
        if(curr_page[goods_type] == 0){
            get_database_pagination(goods_type, Math.ceil(res.iTotalDisplayRecords/query_data.limit));
        }
        box.html('');
        $.each(res.aaData, function (index, value) {
            box.append('<li class="datalist-item"><a href="javascript:void(0);" onclick="to_custom_list(\''+ value.Id +'\',\'lens\', this)"><span>'+ productGroupName(value) +'</span></a></li>');
        });
    }, 'json');
}
function get_database_pagination(goods_type, count) {
    new Pagination({
        wrap: $('.am-pagination-'+goods_type), // 存放分页内容的容器
        count: count, // 总页数
        current: 1, // 当前的页数（默认为1）
        prevText: '上一页', // prev 按钮的文本内容
        nextText: '下一页', // next 按钮的文本内容
        callback: function(page) { // 每一个页数按钮的回调事件
            curr_page[goods_type] = page-1;
            ajax_database_products_list(goods_type);
        }
    });
}

function to_custom_list(product_id, good_type, that) {
    var _this = $(that);
    var title = _this.find('span').text();
    parent.layer.open({
        type: 2
        , title: title+'--定制参数'
        , shade: 0.2
        , id: 'custom_product_list_new_url'
        , area: ['1000px', '700px']
        , content: [custom_product_list_save_url+'?product_id='+product_id]
        , anim: 1 //动画类型
    });
}


//获取配置
$.post('{{ urlfor "ProductsController.GetOptions"}}', {'fields':['goods_lens_refractivity', 'goods_lens_optical_design', 'goods_function_info', 'goods_film']},  function(res){
    setattroption($('select[name="refractivity"]'),res.data.goods_lens_refractivity); //折射率
    setattroption($('select[name="optical_design"]'),res.data.goods_lens_optical_design); //光学设计
    setattroption($('select[name="function_info"]'),res.data.goods_function_info); //功效
    setattroption($('select[name="film"]'),res.data.goods_film); //膜层
});
function setattroption(obj,data) {
    var option = '<option value=""></option>';
    $.each(data, function(index, value) {
        option += '<option value="'+ value.val +'">'+ value.val +'</option>';
    });
    $(obj).html(option);
}
</script>
